<?php
class factureModel{
	private $id,$date,$devis;
	
	public function getId()
	{
		return $this->id;
	}
	
	public function getDate()
	{
		return $this->date;
	}
	
	public function getDevis()
	{
		return $this->devis;
	}
	
	public function setId($value)
	{
		$this->id = $value;
	}

	public function setDate($value)
	{
		$this->date = $value;
	}
	
	public function setDevis($value)
	{
		$this->devis = $value;
	}
	
	public function getFacture($nbrFacture)
	{
		require_once "userModel.php";
		$user = new UserModel();
		$tab_facture = array();
		$sql = "SELECT idFacture,DATE_FORMAT(dateFacture,'%d-%m-%Y') as dateFacture,d.idDevis,montant,DATE_FORMAT(date,'%d-%m-%Y') as date,idClient,idUtilisateur FROM facture f,devis d WHERE f.idDevis=d.idDevis ORDER BY idFacture LIMIT $nbrFacture";
		$req = mysql_query($sql);
		$i =0;
		while($row = mysql_fetch_array($req))
		{
			$date_en = explode("-",$row["date"]);

			$annee = $date_en[0];
			$mois = $date_en[1];
			$jour = $date_en[2];
			
			$dateFr = $jour."/".$mois."/".$annee;
			
			$date_en_facture = explode("-",$row["dateFacture"]);

			$annee = $date_en_facture[0];
			$mois = $date_en_facture[1];
			$jour = $date_en_facture[2];
			
			$dateFr = $jour."/".$mois."/".$annee;
			
			$tab_facture[$i]["id"] = $row["idFacture"];
			$tab_facture[$i]["dateDevis"] = $row["date"];
			$tab_facture[$i]["dateFacture"] = $row["dateFacture"];
			$tab_facture[$i]["devis"] = $row["idDevis"];
			$tab_facture[$i]["montant"] = $row["montant"];
			$tab_facture[$i]["client"] = $row["idClient"];
			$tab_facture[$i]["utilisateur"] = $user->getUser($row["idUtilisateur"]);
			$i++;
		}
		
		return $tab_facture;
	}
	
	public function deletefacture($id_facture)
	{
		$sql = "DELETE FROM facture WHERE idFacture='".$id_facture."' ";
		$req = mysql_query($sql);
		
		if($req)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function createFacture($facture)
	{
		$sql = "INSERT INTO facture (dateFacture,idDevis) VALUES ('".$facture->getDate()."','".$facture->getDevis()."')";
		$req = mysql_query($sql);
				
		if($req)
		{
			$facture->setId(mysql_insert_id());
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
}